package com.ghsc.ghxc.jsb.ui.live

import android.Manifest
import android.graphics.Typeface
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.animation.AnimationUtils
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.adapter.PagerAdapter
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.event.SchedulerTransformer
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setOnFastClickListener
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.flyco.tablayout.listener.OnTabSelectListener
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.CustSignKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.ActivityLiveVideoBinding
import com.ghsc.ghxc.jsb.ui.live.activity.LiveCreateActivity
import com.ghsc.ghxc.jsb.ui.live.fragment.GHVideoListFragment
import com.ghsc.ghxc.jsb.ui.live.fragment.LiveFragment
import com.ghsc.ghxc.jsb.ui.live.fragment.VideoListFragment
import com.ghsc.ghxc.jsb.ui.live.view.PutTypeView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.permissionx.guolindev.PermissionX
import java.util.ArrayList

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.live
 * 类名：TabLiveVideoActivity
 * 创建时间：2024/5/10 10:17
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class TabLiveVideoActivity : BaseVMActivity<TabLiveVideoViewModel, ActivityLiveVideoBinding>() ,
    OnTabSelectListener {
    //精选滑动标识
    val CUST_SIGN_SIFT_TAB =222222


    override fun attachLayoutRes(): Int {
        return R.layout.activity_live_video
    }

    override fun attachVMClass(): Class<TabLiveVideoViewModel>? {
        return TabLiveVideoViewModel::class.java
    }

    var mList: MutableList<String> = ArrayList()
    var mAdapter: PagerAdapter? = null
    private val mFragments: MutableList<Fragment> = ArrayList()


    var index =1
    var bundle :Bundle?=null

    override fun initView() {

//        bundle=intent.extras
//        if (bundle!=null)
//            index=bundle!!.getInt("index",1)

        mList.clear()
        mList.add("直播")
        mList.add("推荐")
        mList.add("走进国华")
        mFragments.clear()
        LiveFragment.getInstance(1,)?.let { mFragments.add(it) }
        VideoListFragment.getInstance(2,)?.let { mFragments.add(it) }
        GHVideoListFragment.getInstance(3,)?.let { mFragments.add(it) }
        mAdapter = PagerAdapter(
            supportFragmentManager,
            mList,
            mFragments
        )
        binding.vpType.adapter = mAdapter
        binding.vpType.offscreenPageLimit = mList.size - 1
        binding.slitab.setViewPager(binding.vpType)
        binding.slitab.setOnTabSelectListener(this@TabLiveVideoActivity)
        binding.slitab.currentTab =index
        binding.vpType.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                tabIndex(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        tabIndex(index)
    }

    override fun onTabSelect(position: Int) {
        tabIndex(position)
    }

    override fun onTabReselect(position: Int) {
    }

    private fun tabIndex(index: Int) {
//        for (i in mList.indices) {
//            if (i == index) {
//                val tv = binding.slitab.getTitleView(i)
//                tv.textSize = 17f
//                tv.typeface = Typeface.defaultFromStyle(Typeface.BOLD) //加粗
//            } else {
//                val tv2 = binding.slitab.getTitleView(i)
//                tv2.textSize = 16f
//                tv2.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
//            }
//        }

        when(index){
            0 ->{
                binding.tvTextLive.typeface= Typeface.defaultFromStyle(Typeface.BOLD)
                binding.tvStateLive.visibility= View.VISIBLE

                binding.tvTextVideo.typeface= Typeface.defaultFromStyle(Typeface.NORMAL)
                binding.tvStateVideo.visibility= View.INVISIBLE

                binding.tvTextThis.typeface= Typeface.defaultFromStyle(Typeface.NORMAL)
                binding.tvStateThis.visibility= View.INVISIBLE

            }
            1 ->{
                binding.tvTextLive.typeface= Typeface.defaultFromStyle(Typeface.NORMAL)
                binding.tvStateLive.visibility= View.INVISIBLE

                binding.tvTextVideo.typeface= Typeface.defaultFromStyle(Typeface.BOLD)
                binding.tvStateVideo.visibility= View.VISIBLE

                binding.tvTextThis.typeface= Typeface.defaultFromStyle(Typeface.NORMAL)
                binding.tvStateThis.visibility= View.INVISIBLE
            }
            2 ->{
                binding.tvTextLive.typeface= Typeface.defaultFromStyle(Typeface.NORMAL)
                binding.tvStateLive.visibility= View.INVISIBLE

                binding.tvTextVideo.typeface= Typeface.defaultFromStyle(Typeface.NORMAL)
                binding.tvStateVideo.visibility= View.INVISIBLE

                binding.tvTextThis.typeface= Typeface.defaultFromStyle(Typeface.BOLD)
                binding.tvStateThis.visibility= View.VISIBLE
            }
        }



        RxBus.getDefault().post(
            RxBusBean(
                CUST_SIGN_SIFT_TAB,
                CUST_SIGN_SIFT_TAB,
                index
            )
        )



    }

    override fun initData() {
        super.initData()
//        ObjectAnimator animator=ObjectAnimator.ofFloat(img,"ScaleX",1.0F,2.0F,1.0F);//图片默认X轴不变然后放大2倍然后恢复原样
//        72         animator.setDuration(4000);//缩放动画持续时间为4000毫秒
//        73         animator.start();//开始执行动画(图片横向放大2倍动画)


//         var animator=ObjectAnimator.ofFloat(binding.tvTextLive,0.5f,0.5f,0.5f,0.5f)
        val anim = AnimationUtils.loadAnimation(this,R.anim.an_live_tv)
        binding.tvTextLive.animation = anim
    }

    override fun initClickListener() {
        super.initClickListener()
        binding.llBtnLive.setSingleClickListener {
//            tabIndex(0)
            binding.slitab.currentTab =0
        }
        binding.llBtnVideo.setSingleClickListener {
//            tabIndex(1)
            binding.slitab.currentTab =1
        }
        binding.llBtnThis.setSingleClickListener {
//            tabIndex(2)
            binding.slitab.currentTab =2
        }
        binding.imgBtnPut.setSingleClickListener {
            XPopup.Builder(this@TabLiveVideoActivity)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter  )
                .customHostLifecycle(lifecycle)
                .navigationBarColor(getCompatColor(R.color.bg_dark_color))
                .asCustom(
                    PutTypeView(this@TabLiveVideoActivity,
                        tvBtnSub = {
                            if (it==0){
                                showToast("暂未开放")
                            }else {
//                                showToast("跳转开始直播")
                                mViewModel.onGetzhiboAuthMethod("")
                            }
                        }
                    )
                )
                .show()
        }
        binding.imgBtnLeft.setSingleClickListener {
            finish()
        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@TabLiveVideoActivity) {
                showToast(it)
            }
            againLoginMsg.observe(this@TabLiveVideoActivity) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }
            onGetzhiboAuthData.observe(this@TabLiveVideoActivity){
                startActivity(LiveCreateActivity().javaClass)
            }
        }
    }

}